import type { ApiPriceComponent } from '@/types';
import { basePriceComponent, timeout } from './constants';

export const postPriceComponents = (
  priceComponents: ApiPriceComponent[],
  shouldApiFail: boolean
): Promise<void> =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      if (shouldApiFail) {
        reject(new Error('postPriceComponents failed'));
      } else {
        resolve();
      }
    }, timeout);
  });

export const putPriceComponents = async (
  priceComponent: ApiPriceComponent,
  shouldApiFail: boolean
): Promise<void> =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      if (shouldApiFail) {
        reject(new Error('putPriceComponents failed'));
      } else {
        resolve();
      }
    }, timeout);
  });

export const getPriceComponents = (
  shouldApiFail: boolean
): Promise<ApiPriceComponent[]> =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      if (shouldApiFail) {
        reject(new Error('getPriceComponents failed'));
      } else {
        resolve([basePriceComponent]);
      }
    }, timeout);
  });

export const deletePriceComponents = (
  priceComponents: ApiPriceComponent[],
  shouldApiFail: boolean
): Promise<void> =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      if (shouldApiFail) {
        reject(new Error('deletePriceComponents failed'));
      } else {
        resolve();
      }
    }, timeout);
  });
